package com.banach.prema.executor;

import com.alibaba.fastjson.JSONObject;
import com.banach.prema.dto.vo.ImTokenVO;
import com.banach.prema.im.config.AppConfig;
import com.banach.prema.im.config.TlsConfig;
import com.banach.prema.im.util.RestApiUtils;
import com.ego.framework.core.context.UserHolder;
import com.ego.framework.dto.Response;
import com.ego.framework.model.LoginUser;
import com.ego.ucenter.api.UserRemoteService;
import com.ego.ucenter.dto.vo.user.UserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @Author ajun
 * @Desc
 * @Date 2025/3/24 11:33
 */
@Component
public class ImTokenGenExe {
    @Resource
    private AppConfig appConfig;
    @Resource
    private TlsConfig tlsConfig;
    @Resource
    private RestApiUtils restApiUtils;
    @Resource
    private UserRemoteService userRemoteService;
    
   public Response<ImTokenVO> genImToken(){
       LoginUser loginUser = UserHolder.getLoginUser();
       UserVO userVO = userRemoteService.selectById(loginUser.getId()).get();
       //账号导入云通信服务器
       JSONObject postData = new JSONObject();
       postData.put("Identifier", loginUser.getMobile());
       postData.put("Nick", loginUser.getName());
       postData.put("FaceUrl", userVO.getAvatar());
       JSONObject res = restApiUtils.doPost("v4/im_open_login_svc/account_import", postData);
       ImTokenVO token = new ImTokenVO();
       String usersig = tlsConfig.getUsersig(loginUser.getMobile());
       token.setUsersig(usersig)
               .setSdkAppId(appConfig.getSdkAppId())
                .setUserId(loginUser.getMobile());
       return Response.of(token);
    }
}
